

debug_mode = True 

def debug_print(s):
    if debug_mode:
        print(s)
    

def dfs(start_vertex, graph):
    
    visited = {} # the set of vertices that are or have been on the stack 
    number_of_visited_out_edges = {} # for each vertex u, the number of outgoing edges (u,v) we have looked at 

    for u in graph:
        number_of_visited_out_edges[u] = 0
        visited[u] = False 

    stack = [start_vertex] # this is a path starting at start_vertex
    visited[start_vertex] = True

    while (len(stack) > 0):
        u = stack[-1] # top element of the stack
        i = number_of_visited_out_edges[u] # what is the next unexplored edge out of u?
        if i == len(graph[u]): # then all out-edges of u have already been explored
            stack.pop() # remove u from stack, i.e., backtrack 
            debug_print(f"popping {u} from the stack")
        else: 
            v = graph[u][i] # edge (u,v) is the next unexplored out-edge of u 
            number_of_visited_out_edges[u] += 1 
            debug_print(f"taking edge {(u,v)}")
            if not visited[v]:
                stack.append(v) # advancing the depth-first search 
                visited[v] = True 
                debug_print(f"pusing {v} onto stack")
            else:
                debug_print(f"{v} already visited")


# read the first line: number of vertices and edges
(n, m) = list(map(int, input().split()))
out_neighbors = {}
in_neighbors = {}

# now initialize each adjacency list as an empty list 
for u in range(1,n+1): 
    out_neighbors[u] = []
    in_neighbors[u] = []
# the above code would need to be adapted e.g. if some problem indexes the vertices from 0 to n-1

# now read all edges
for edge_index in range(m):
    (u, v) = list(map(int, input().split()))
    out_neighbors[u].append(v)
    in_neighbors[v].append(u)

# read the start vertex 
start_vertex = int(input())
dfs(start_vertex, out_neighbors)
            

        

